<template>
    <Dialog v-model="dialogVisible" :title="dialogTitle">
        <el-row>
            <el-col>
                <el-input v-model="userName" class="mb-20px" clearable placeholder="请输入用户名称">
                    <template #prefix>
                        <Icon icon="ep:search" />
                    </template>
                </el-input>
            </el-col>
            <el-col>
                <el-table v-loading="loading" :data="list" row-key="id" @selection-change="handleSelectionChange"
                    ref="userTable">
                    <el-table-column type="selection" label="" align="center"  :selectable="selectableStatus" :reserve-selection="true" width="55" />
                    <el-table-column label="姓名" align="center" prop="userName" :show-overflow-tooltip="true" />
                    <el-table-column label="手机号" align="center" prop="phone" width="120" />
                    <el-table-column label="工作单位" align="center" prop="workUnit" :show-overflow-tooltip="true" />
                    <el-table-column label="负责区域" align="center" prop="regionAddress" :show-overflow-tooltip="true" />
                    <el-table-column align="center" label="启用" prop="isEnable" width="100">
                        <template #default="scope">
                            <el-switch v-model="scope.row.isEnable" active-value="1" inactive-value="0" disabled />
                        </template>
                </el-table-column>
                </el-table>
            </el-col>
            <el-col>
                <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
                    @pagination="getList" />
            </el-col>
        </el-row>
        <template #footer>
            <el-button :disabled="loading" type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="dialogVisible = false">取 消</el-button>
        </template>
    </Dialog>
</template>
<script lang="ts" setup>
import * as groupApi from '@/api/infoArrive/arriveNotice/userGroup'

defineOptions({ name: 'UserForm' })
const props = defineProps({
    oldUserIds: String
})
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const userName = ref(undefined)
const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    regionAddress:''
})
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}
const userTable = ref()
const toggleSelection = (rows) => {
    if (rows!='' && list.value.length != 0) {
        //遍历table表格数据与要回显的数组比对，匹配成功的设置为选中状态
        list.value.forEach((row: any) => {
            rows.split(',').forEach((item) => {
                if (row.id == item) {
                    row.disabled = true
                    //判断唯一标识，若是id一样，则设置该行选中状态为true
                    userTable.value.toggleRowSelection(row, true)
                }
            })
        })
    }
}
const selectableStatus = (row)=>{
    return !row.disabled
}

/** 打开弹窗 */
const open = async () => {
    dialogVisible.value = true
    dialogTitle.value = '新增'
    reset()
    await getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['querySelect']) // 定义 querySelect 事件，用于操作成功后的回调
const submitForm = async () => {
    if (multipleSelection.value.length == 0) {
        message.warning('请先勾选用户！！')
        return
    }
    dialogVisible.value = false
    // 提交请求
    // 发送操作成功的事件
    emit('querySelect', multipleSelection.value)
}

/** 查询列表 */
const getList = async () => {
    loading.value = true
    try {
        const data = await groupApi.getUserList({ ...queryParams, userName: userName.value })
        list.value = data.list
        total.value = data.total
        props.oldUserIds&&toggleSelection(props.oldUserIds)
    } finally {
        loading.value = false
    }
}
/** 重置表单 */
const reset = () => {
    userName.value = undefined;
    queryParams.pageNo = 1;
    queryParams.pageSize = 10;
}
/** userName */
watch(userName, () => {
    queryParams.pageNo = 1;
    getList()
})
</script>